/*
** ADOBE SYSTEMS INCORPORATED
** Copyright 2012 Adobe Systems Incorporated
** All Rights Reserved.
**
** NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the
** terms of the Adobe license agreement accompanying it.  If you have received this file from a
** source other than Adobe, then your use, modification, or distribution of it requires the prior
** written permission of Adobe.
*/
package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.IOErrorEvent;
	import flash.events.AsyncErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.system.Security;
	import flash.external.ExternalInterface;
	
	import starling.core.Starling;

	import sample.lua.CModule;
	
	[SWF(width="640", height="960", frameRate="60", backgroundColor="#ffffff")]
	public class LuaStarling extends Sprite
	{
		private var _width:Number = 640
		private var _height:Number = 960
		private var mStarling:Starling
		private var scriptloader:URLLoader
		
		include "../../TestingCode.as";

		public function write(fd:int, buf:int, nbyte:int, errno_ptr:int):int
		{
			var str:String = CModule.readString(buf, nbyte)
			trace(str)
			return nbyte
		}

		public function LuaStarling()
		{
			trace("LuaStarling ctor");
			addEventListener(Event.ADDED_TO_STAGE, onAdded)
		}

		private function onAdded(e:*):void 
		{
			stage.frameRate = 60
			
			// Initialize the flascc world
			CModule.startAsync(this)

			if (ExternalInterface.available) {
				try {
					ExternalInterface.addCallback("newLuaScript", newLuaScript);
					Security.allowDomain("*") // Awwww yeah!
					trace("External interface callback added.");
				} catch (error:Error) {
					trace("An Error occurred: " + error.message + "\n");
				}
			} else {
				trace("External interface is not available for this container.");
				scriptloader = new URLLoader(new URLRequest("game.lua"))
				scriptloader.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onError)
				scriptloader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError)
				scriptloader.addEventListener(IOErrorEvent.IO_ERROR, onError)
				scriptloader.addEventListener(Event.COMPLETE, onComplete)
			}

			// Initialize the Starling world
			stage.scaleMode = StageScaleMode.NO_SCALE
			stage.align = StageAlign.TOP_LEFT
			stage.quality = StageQuality.LOW
			if (Capabilities.manufacturer.toLowerCase().indexOf("ios") != -1 || 
				Capabilities.manufacturer.toLowerCase().indexOf("android") != -1)
			{
				_width = Capabilities.screenResolutionX
				_height = Capabilities.screenResolutionY
			}

			Starling.multitouchEnabled = true
			initTesting();
		}

		private function onComplete(e:*):void
		{
			LuaGame.luascript = scriptloader.data
			scriptloader = null
			initStarling()
		}

		private function initStarling():void
		{
			if(mStarling) {
				mStarling.stop()
				mStarling.dispose()
			}
		
			mStarling = new Starling(LuaGame, stage, new Rectangle(0, 0, _width, _height))
			mStarling.simulateMultitouch = true
			mStarling.enableErrorChecking = false
			mStarling.start()
		}
		
		private function newLuaScript(value:String):void
		{
			LuaGame.luascript = value
			initStarling();
		}
		
		public static function onError(e:*):void
		{
			if(ExternalInterface.available) {
				ExternalInterface.call("reportError", e.toString())
			} else {
				trace(e)
			}
		}
	}
}
